﻿using System.Collections.Generic;
using System.Linq;
using MC.RideEvent.Mvc.Models.Entity;
using System.Web.Mvc;
using MC.RideEvent.Mvc.Models.ViewModel;

namespace MC.RideEvent.Mvc.Models.Repository
{
    public static class SelectListRepository
    {
        private const int MAX_NUMBER_PLACES = 5;

        private static RideEventDBEntities _context = new RideEventDBEntities();

        public static SelectList ListSexe()
        {
            List<object> items = new List<object>();
            items.Add( new { value = "H", text = Sexe.Homme.ToString()} );
            items.Add(new { value = "F", text = Sexe.Femme.ToString() });

            var list = new SelectList(items, "value", "text");

            return list;
        }

        /// <summary>
        /// This return a list of item in order to
        /// fill SelectList of "Way" and "Preference" form.
        /// "nbPlacesMax" refer to available number place of a vehicule.
        /// "selectedVal" refer to value which will be selected. 
        /// </summary>
        /// <param name="nbPlacesMax"></param>
        /// <param name="selectedVal"></param>
        /// <returns></returns>
        public static SelectList ListNumberPlaces(int nbPlacesMax, object selectedVal)
        {
            List<object> items = new List<object>();

            for (int i = 1; i <= nbPlacesMax; i++)
            {
                items.Add(new SelectListItem { Value = i.ToString(), Text = i.ToString(), Selected = i == (int)selectedVal });
            }

            var list = new SelectList(items, "Value", "Text");

            return list;
        }

        /// <summary>
        /// This methode used only for "Vehicule" form
        /// with a default value : "MAX_NUMBER_PLACES";
        /// </summary>
        /// <param name="selectedVal"></param>
        /// <returns></returns>
        public static SelectList ListNumberPlaces(int selectedVal)
        {
            List<object> items = new List<object>();

            for (int i = 1; i <= MAX_NUMBER_PLACES; i++)
            {
                var item = new SelectListItem
                               {
                                   Value = i.ToString(), 
                                   Text = i.ToString(), 
                                   Selected = selectedVal == i
                               };

                items.Add(item);
            }

            var list = new SelectList(items, "Value", "Text");

            return list;
        }

        public static SelectList ListNumberPlacesWithMax(int nbPlacesMax)
        {
            var sl = ListNumberPlaces(nbPlacesMax, 0);
            return sl;
        }

        /*
         //public static SelectList ListNumberPlaces(int nbPlaceMax)
        public static SelectList ListNumberPlaces(int selectedVal)
        {
            List<object> items = new List<object>();

            nbPlaceMax = nbPlaceMax > 0 ? nbPlaceMax : MAX_NUMBER_PLACES; 

            for (int i = 1; i <= nbPlaceMax; i++)
            {
                var item = new SelectListItem {Value = i.ToString(), Text = i.ToString()};
                items.Add(item);
            }

            var list = new SelectList(items, "Value", "Text");

            return list;
        }
         */

        public static SelectList ListOuiNon(object selectVal)
        {
            List<object> items = new List<object>();
            items.Add(new SelectListItem {Value = false.ToString(), Text = "Non" });
            items.Add(new SelectListItem { Value = true.ToString(), Text = "Oui" });

            var list = new SelectList(items, "Value", "Text", selectVal);

            return list;
        }

        public static SelectList ListDriverStyle()
        {
            List<object> listItems = new List<object>();
            var items = (from dt in _context.DriverStyles
                         select dt).ToList();

            foreach (var driverStyle in items)
            {
                listItems.Add(new SelectListItem { Value = driverStyle.Id.ToString(), Text = driverStyle.Title });
            }

            var sl = new SelectList(listItems, "Value", "Text");

            return sl;
        }
    }
}